enyo.kind({
    name: "MyApps.News",
    kind: enyo.VFlexBox,
    events: {
        onUnreadCount: "",
    },
    components: [    /**
     * Services
     */
    {
        name: "getFeed",
        kind: "WebService",
        url: "http://query.yahooapis.com/v1/public/yql?q=select%20title%2C%20description%2C%20link%20from%20rss%20where%20url%3D%22http://www.golf-zone.com/ninjarsssyndicator%22&format=json&callback=",
        onSuccess: "gotFeed",
        onFailure: "gotFeedFailure",
    }, {
        name: 'myDbService',
        kind: "DbService",
        dbKind: "fr.mobile2you.golfplus.tablet.news:1",
        onFailure: "dbFailure",
        subscribe: false
    }, {
        name: "pane",
        kind: "Pane",
        flex: 1,
        transitionKind: "enyo.transitions.LeftRightFlyin",
        components: [{
            kind: "VFlexBox",
            components: [{
                kind: "Scroller",
                flex: 1,
                className: "unreadlist",
                components: [{
                    name: "list",
                    kind: "VirtualRepeater",
                    onSetupRow: "getListItem",
                    components: [{
                        name: "newsitem",
                        kind: "Item",
                        layoutKind: "VFlexLayout",
                        components: [{
                            name: "title",
                            kind: "Divider"
                        }, {
                            name: "description",
                            kind: "HtmlContent",
                            onLinkClick: "doLinkClick"
                        }],
                        onclick: "listItemClick"
                    }]
                }]
            }, {
                kind: "Image",
                src: "images/GolfZoneBanner.png",
                style: "padding-left: 0%"
            }, ]
        
        }, {
            name: "newsdetail",
            kind: "MyApps.WebWithHeader",
            lazy: true,
            onBack: "goBack"
        }]
    }, ],
    
    
    create: function(){
        this.inherited(arguments);
        this.results = [];
        this.unreadcount = 0;
        this.createDb();
        this.loadFeed();
    },
    goBack: function(inSender, inEvent){
        this.$.pane.back(inEvent);
    },
    
    loadFeed: function(){
        this.$.getFeed.call();
    },
    gotFeed: function(inSender, inResponse){
        this.results = inResponse.query.results.item;
        this.getFromDb();
    },
    
    gotFeedFailure: function(inSender, inResponse){
        console.log("failure from getFeed");
    },
    
    getListItem: function(inSender, inIndex){
        var r = this.results[inIndex];
        if (r) {
            if (r.isread) {
                this.$.description.setClassName("readitem");
            }
            this.$.title.setCaption(r.title);
            this.$.description.setContent(r.description);
            return true;
        }
    },
    listItemClick: function(inSender, inEvent){
        var feed = this.results[inEvent.rowIndex];
        //mark as read after click
        this.results[inEvent.rowIndex].isread = true;
        this.$.list.render();
        
        this.$.pane.selectViewByName("newsdetail");
        this.$.newsdetail.setUrl(feed.link);
        
        
        //mark as read and insert into DB		
        this.insertIntoDb(feed.title);
        
    },
    doLinkClick: function(inSender, inUrl){
        this.$.pane.selectViewByName("newsdetail");
        this.$.newsdetail.setUrl(inUrl);
    },
    
    
    /*
     * Already Read News
     *
     */
    /**
     * Fires event with unread news count
     */
    fireChangeUnreadCountEvent: function(){
        this.doUnreadCount(this.unreadcount);
    },
    
    /**
     * make object which will be stored in DB
     */
    formatReadNewsData: function(){
        var a = [];
        var dbKind = this.$.myDbService.dbKind;
        for (var i = 0, f; f = this.results[i]; i++) {
            if (f.isread) {
                a.push({
                    _kind: dbKind,
                    name: f.title
                });
            }
        }
        return a;
    },
    
    
    /**
     * check which news were read and mark them (isread = true), compare condition is news title
     * @param {Object} data - data from database(news titles)
     */
    checkWhichWereRead: function(data){
        this.unreadcount = this.results.length;
        
        for (var i = 0; i < this.results.length; i++) {
            var newstitle = this.results[i].title;
            
            for (var j = 0; j < data.results.length; j++) {
                if (data.results[j].name == newstitle) {
                    this.results[i].isread = true;
                    this.unreadcount--;
                }
            }
        }
        this.fireChangeUnreadCountEvent();
    },
    
    
    
    /*
     * Create DB
     */
    createDb: function(){
        this.$.myDbService.call({
            owner: enyo.fetchAppId()
        }, {
            method: 'putKind',
            onSuccess: 'putKindSuccess',
            onFailure: 'putKindFailure'
        });
    },
    
    putKindFailure: function(inSender, inResponse){
        console.log("putKindFailure");
    },
    putKindSuccess: function(inSender, inResponse){
        console.log("putKindSuccess");
    },
    
    /*
     * Insert Into DB
     */
    insertIntoDb: function(title){
    
        this.deleteAllFromDb();
        this.unreadcount--;
        this.fireChangeUnreadCountEvent();
        
        this.$.myDbService.call({
            objects: this.formatReadNewsData()
        }, {
            method: 'put',
            onSuccess: 'insertSuccess',
            onFailure: 'insertFailure'
        });
    },
    
    insertSuccess: function(inSender, inResponse){
        console.log("insertSuccess");
    },
    insertFailure: function(inSender, inResponse){
        console.log("insertFailure");
    },
    
    /*
     * Delete from
     */
    deleteAllFromDb: function(){
        this.$.myDbService.call({
            query: {
                "from": "fr.mobile2you.golfplus.tablet.news:1"
            },
            purge: true
        }, {
            method: 'del',
            onSuccess: 'deleteSuccess',
            onFailure: 'deleteFailure'
        });
    },
    
    deleteSuccess: function(inSender, inResponse){
        console.log("deleteSuccess");
    },
    
    deleteFailure: function(inSender, inResponse){
        console.log("deleteFailure");
    },
    
    
    /*
     * Load data from DB
     */
    getFromDb: function(){
        this.$.myDbService.call({
            query: ''
        }, {
            method: 'find',
            onSuccess: 'getSuccess',
            onFailure: 'getFailure'
        });
    },
    
    getSuccess: function(inSender, data){
        this.checkWhichWereRead(data);
        this.$.list.render();
    },
    getFailure: function(inSender, inResponse){
        console.log("getFailure");
    },


});

